#! /usr/bin/python3
# -*- coding: UTF-8 -*-

if __name__ == "__main__":
	raise Exception("不支持从这里调用")

__version__ = "1.0"
__all__ = ["MouseChshDateTime"]

from datetime import datetime, timedelta, timezone
from dateutil.relativedelta import relativedelta

from com.mousechsh.common.log.MouseChshLog import mousechsh_logging_exception


class MouseChshDateTime:

	def __init__(self, *, tz=8):
		self.__time = datetime.now()
		self.__tz = timezone(timedelta(hours=tz))
		self.__time = self.__time.replace(tzinfo=self.__tz)
		self.__format = '%Y-%m-%d %H:%M:%S'

	def set_timezone(self, value):
		try:
			if isinstance(value, int):
				self.__tz = timezone(timedelta(hours=value))
				self.__time = self.__time.astimezone(self.__tz)
			elif value == 'UTC':
				self.__tz = timezone(timedelta(hours=0))
				self.__time = self.__time.astimezone(self.__tz)
			elif value == 'Asia/Shanghai':
				self.__tz = timezone(timedelta(hours=8))
				self.__time = self.__time.astimezone(self.__tz)
			elif value == 'Asia/Tokyo':
				self.__tz = timezone(timedelta(hours=9))
				self.__time = self.__time.astimezone(self.__tz)
		except Exception as ex:
			mousechsh_logging_exception(ex, '设置时区时遇到错误：')

	def set_timezone_(self, value):
		self.set_timezone(value)
		return self

	def from_string(self, value):
		try:
			self.__time = datetime.strptime(value, self.__format)
			self.__time = self.__time.replace(tzinfo=self.__tz)
		except Exception as ex:
			mousechsh_logging_exception(ex, '从字符串转换时间时遇到错误：')

	def from_string_(self, value):
		self.from_string(value)
		return self

	def to_string(self):
		return self.__time.strftime(self.__format)

	def from_timestamp(self, stamp):
		try:
			self.__time = datetime.fromtimestamp(stamp, self.__tz)
		except Exception as ex:
			mousechsh_logging_exception(ex, '从时间戳转换时间时遇到错误：')

	def from_timestamp_(self, stamp):
		self.from_timestamp(stamp)
		return self

	def to_timestamp(self):
		return int(self.__time.timestamp())

	def set_mode(self, value):
		if value == 'yyyyMMddHHmmss':
			self.__format = '%Y-%m-%d %H:%M:%S'
		elif value == 'ymdhms':
			self.__format = '%Y-%m-%d %H:%M:%S'
		elif value == 'yyyyMMdd':
			self.__format = '%Y-%m-%d'
		elif value == 'ymd':
			self.__format = '%Y-%m-%d'
		elif value == 'yyyyMM':
			self.__format = '%Y-%m'
		elif value == 'ym':
			self.__format = '%Y-%m'

	def set_mode_(self, value):
		self.set_mode(value)
		return self

	def add(self, pos, value):
		try:
			pos = str(pos).lower()
			value = int(value)
			if pos == 'year':
				self.__time = self.__time + relativedelta(years=value)
			elif pos == 'month':
				self.__time = self.__time + relativedelta(months=value)
			elif pos == 'day':
				self.__time = self.__time + timedelta(days=value)
			elif pos == 'hour':
				self.__time = self.__time + timedelta(hours=value)
			elif pos == 'minute':
				self.__time = self.__time + timedelta(minutes=value)
			elif pos == 'second':
				self.__time = self.__time + timedelta(seconds=value)
		except Exception as ex:
			mousechsh_logging_exception(ex, '增加减少指定时间间隔时遇到错误：')

	def add_(self, pos, value):
		self.add(pos, value)
		return self

	def get(self, pos):
		pos = str(pos).lower()
		if pos == 'year':
			return self.__time.year
		elif pos == 'month':
			return self.__time.month
		elif pos == 'day':
			return self.__time.day
		elif pos == 'hour':
			return self.__time.hour
		elif pos == 'minute':
			return self.__time.minute
		elif pos == 'second':
			return self.__time.second

	def set(self, pos, value):
		try:
			pos = str(pos).lower()
			value = int(value)
			if pos == 'year':
				self.__time = self.__time + relativedelta(year=value)
			elif pos == 'month':
				self.__time = self.__time + relativedelta(month=value)
			elif pos == 'day':
				self.__time = self.__time + relativedelta(day=value)
			elif pos == 'hour':
				self.__time = self.__time + relativedelta(hour=value)
			elif pos == 'minute':
				self.__time = self.__time + relativedelta(minute=value)
			elif pos == 'second':
				self.__time = self.__time + relativedelta(second=value)
		except Exception as ex:
			mousechsh_logging_exception(ex, '设置指定时间位时遇到错误：')

	def set_(self, pos, value):
		self.set(pos, value)
		return self

	def clean(self):
		self.from_string(self.to_string())

	def clean_(self):
		self.clean()
		return self
